package com.kgd.news.utils;


import it.sauronsoftware.base64.Base64;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.InetAddress;
import java.security.MessageDigest;
import java.util.UUID;

public class StringUtils {

    /**
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0 || "undefined".equals(str) || "null".equals(str))
            return true;
        else
            return false;
    }

    /**
     * 对字符串md5加密
     *
     * @param str
     * @return
     */
    public static String md5(String str) {
        try {
            // 生成一个MD5加密计算摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            md.update(str.getBytes());
            // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
            // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
            return new BigInteger(1, md.digest()).toString(16);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 生成tokenid
     *
     * @param storeid
     * @return
     */
    public static String generateQRcodeContent(int storeid) {
        String code = UUID.randomUUID().toString() + storeid + Constants.TOKEN_KEY;
        return md5(code);
    }

    /**
     * 生成订单号
     *
     * @return public static String generateOrderId() {
    String orderId = RandomStringGenerator.getRandomStringByLength(31);
    //StoreOrder so = StoreOrder.dao.findFirst("select * from store_order where id = ?", orderId);
    //if(so != null){
    //	return RandomStringGenerator.getRandomStringByLength(31);
    //}
    return orderId;
    }*/

    /**
     * 生成订单号
     *
     * @return public static String createOrderId(int storeid) {
    String random = RandomStringGenerator.getRandoomNumberByLength(3);
    return  DateUtils.getOrderformat()+storeid+random;
    } */


    /**
     * 获取本机的IP
     *
     * @return Ip地址
     */
    public static String getLocalHostIP() {
        String ip;
        try {
            /** 返回本地主机。 */
            InetAddress addr = InetAddress.getLocalHost();
            /** 返回 IP 地址字符串（以文本表现形式） */
            ip = addr.getHostAddress();
        } catch (Exception ex) {
            ip = "";
        }
        return ip;
    }

    /**
     * base64
     *
     * @param code
     * @return
     */
    public static String decodeByBase64(String code) {
        String result = "";
        //try {
        if (!isEmpty(code)) {
            //byte[] data = Base64.decodeBase64(code);
            //result = new String(data, "utf-8");
            result = Base64.decode(code);
        }
        /*} catch (UnsupportedEncodingException e) {
			e.printStackTrace();

		}*/
        return result;
    }


    public static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();
        String line = null;
        try {

            while ((line = reader.readLine()) != null) {

                sb.append(line + "/n");

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                is.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }
        return sb.toString();

    }

}
